/**
 * 表单设计----选择按钮
 */
mini.parse();
var grid = mini.get("datagrid1");

function SetData(data) {
    var data = mini.clone(data);
    var action = data.action;
    var selected;
    if(data.selected != undefined) {
        selected = data.selected.split(',');
    }else {
    	selected = '';
    }

    $.ajax({
        url: ajaxUrl + "/buttons/findByState.do",
        type: "post",
        dataType: "json",
        success: function(grid_data) {
            console.log(grid_data);
            grid.loading();
            grid.setData(grid_data.data);
            //默认选中
            grid.findRows(function (row) {
            	for(var i=0;i<selected.length;i++) {
            		if (row.buttonId == selected[i]) {
                        grid.select(row);
            		}
            	}
            	if(action == 'add'){
                	if(row.buttonId == 1 || row.buttonId == 2 || row.buttonId == 3 || row.buttonId == 4 || row.buttonId == 5) {
                		grid.select(row);
                	}
            	}
            });
//            grid.on("beforeselect", function (e) {
//                if (e.record.buttonId == 1 || e.record.buttonId == 2 ||
//                		e.record.buttonId == 3 ||e.record.buttonId == 4 ||e.record.buttonId == 5){
//                	e.cancel = true;
//                }
//            });
        },
        error : function(data){
            // alert('获取失败');
        }
    });

   
}

//确定按钮之前
function onTableOk() {
    var grid = mini.get("datagrid1");
    var rows = grid.getSelecteds();
//    console.log(rows);
    var gridSelected = [];
    for(var i=0;i<rows.length;i++) {
    	gridSelected.push(rows[i].buttonId);
    }
    if(gridSelected == undefined) {
        mini.alert('请选择按钮！');
    }else {
        return gridSelected+''; //返回选中的数据给父组件
    }
}

//确定按钮
function onOk(e) {
    CloseWindow("ok");
}
//取消按钮
function onCancel(e) {
    CloseWindow("cancel");
}

function CloseWindow(action) {

    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

//表格树数据加载
var dbData = [];//存放转换后的数据库表数据
function dbTree(grid,data) {
    dbData = [];
    console.log(data,'data');
    for(var i=0;i<data.length;i++) {
        dbData[i] = {
            tableClassName: data[i].tableClassName,
            tableChClassName: data[i].tableChClassName,
            cTableName: data[i].tableChClassName,
            field: '',
            fieldCh: '',
            tableClassId: data[i].tableClassId + '',//id
            PID: -1,
            checked: false,
            expanded: false,
            children:[]
        };
        for(var j=0;j<data[i].createTables.length;j++) {
            var c = data[i].createTables[j];
            dbData[i].children.push({
                // cTableField: c.field,
                cTableName: data[i].tableChClassName,
                tableClassName: data[i].tableClassName,
                // formListId: data[i].formListId,
                type: c.type,
                field: c.field,
                fieldCh: c.fieldCh,
                tableClassId: c.tableId + '',//id
                PID: data[i].tableClassId + '',
                expanded: false,
                checked: false
            });
        }
    }
    console.log(dbData,'dbData');
    grid.setData(dbData);
    return dbData;
}